package kitwee.com.kanpad.orderDetail;


import android.content.Context;
import android.content.SharedPreferences;

import com.kitwee.serialport.data.model.SerialPortParam;

public final class SerialPortPreferences {

    private static SharedPreferences PREFERENCES;

    private static final Object mLock = new Object();

    private SerialPortPreferences() {
    }

    public static void initialize(Context context) {
        synchronized (mLock) {
            if (PREFERENCES == null) {
                PREFERENCES = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
            }
        }
    }

    public static String getDevicePath() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_DEVICE, null);
    }

    public static void saveDeivcePath(String devicePath) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_DEVICE, devicePath).apply();
    }

    public static String getBaudRate() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_BAUDRATE, null);
    }

    public static void saveBaudRate(String baudRate) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_BAUDRATE, baudRate).apply();
    }

    public static String getParity() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_PARITY, null);
    }

    public static void saveParity(String parity) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_PARITY, parity).apply();
    }

    public static String getDatabits() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_DATABITS, null);
    }

    public static void saveDeviceId(String deviceId) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_DEVICEID, deviceId).apply();
    }
    public static String getDeviceId() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_DEVICEID, null);
    }
    public static void saveUploadUrl(String urls) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_URL, urls).apply();
    }
    public static String getUploadUrl() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_URL, null);
    }
    public static void saveTimeSpa(String timeSpa) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_TIME, timeSpa).apply();
    }


    public static String getTimeSpa() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_TIME, null);
    }

    public static void saveDatabits(String databits) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_DATABITS, databits).apply();
    }

    public static String getStopbits() {
        checkPreferencesNotNull();
        return PREFERENCES.getString(KEY_STOPBITS, null);
    }

    public static void saveStopbits(String stopbits) {
        checkPreferencesNotNull();
        PREFERENCES.edit().putString(KEY_STOPBITS, stopbits).apply();
    }


    public static SerialPortParam getSerialPortParam() {
        checkPreferencesNotNull();
        String devicePath = getDevicePath();
        if (devicePath == null) {
            return null;
        }
        String bauudrate = getBaudRate();
        if (bauudrate == null) {
            return null;
        }
        String parity = getParity();
        if (parity == null) {
            return null;
        }
        String databits = getDatabits();
        if (databits == null) {
            return null;
        }
        String stopbits = getStopbits();
        if (stopbits == null) {
            return null;
        }
        return new SerialPortParam.Builder()
                .devicePath(devicePath)
                .parity(parity)
                .baudRate(bauudrate)
                .databits(databits)
                .stopbits(stopbits)
                .build();
    }


    private static void checkPreferencesNotNull() {
        if (PREFERENCES == null) {
            throw new NullPointerException("Please call initialize() first!");
        }
    }

    private static final String FILE_NAME = "serial_port";

    private static final String KEY_DEVICE = "device";
    private static final String KEY_BAUDRATE = "bauarate";
    private static final String KEY_DATABITS = "databits";
    private static final String KEY_PARITY = "parity";
    private static final String KEY_STOPBITS = "stopbits";
    private static final String KEY_DEVICEID = "deviceIds";
    private static final String KEY_TIME = "time_spa";
    private static final String KEY_URL = "upload_url";


}
